package BT_47;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;

public class Solution {
	public List<List<Integer>> permuteUnique(int[] nums) {
		List<List<Integer>> res = new LinkedList<List<Integer>>();
		List<Integer> path = new LinkedList<Integer>();
		int len = nums.length;
		Arrays.sort(nums);
		boolean[] used = new boolean[len];
		BT(nums, len, 0, path, used, res);
		return res;
	}

	private void BT(int[] nums, int len, int place, List<Integer> path, boolean[] used, List<List<Integer>> res) {
		// TODO Auto-generated method stub
		if (place == len) {
			res.add(new LinkedList<Integer>(path));
			return;
		}
		for (int i = 0; i < len; i++) {
			if (used[i]) {
				continue;
			}
			if (i > 0 && nums[i] == nums[i - 1] && used[i - 1] == false) {
				continue;
			}

			path.add(nums[i]);
			used[i] = true;
			BT(nums, len, place+1, path, used, res);
			used[i] = false;
			path.remove(path.size() - 1);

		}

	}

}
